#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;

int cargo = 0;
bool shipment_available() {
    return cargo != 0;
}

void consume(int n) {
    for (int i = 0; i < n; ++i) {
        std::unique_lock<std::mutex> lck(mtx);
        cv.wait(lck, shipment_available);
        std::cout << pthread_self()<< ":" << cargo << '\n';
        cargo = 0;
    }
}

int main(int argc, const char **argv) {
    std::thread consumer_thread1(consume, 10);
    std::thread consumer_thread2(consume, 10);

    for (int i = 0; i < 20; ++i) {
        while (shipment_available()) {
            std::this_thread::yield();
        }

        std::unique_lock<std::mutex> lck(mtx);
        cargo = 1 + i;
        cv.notify_one();
        //cv.notify_all();
    }

    consumer_thread1.join();
    consumer_thread2.join();
    return 0;
}
